package app;

import com.utils.LogCustom;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class TestBaseCaseApp {
    public static AndroidDriver driver;
    public boolean isQuitDivices = true;//执行完用例是不是要关闭程序
    //方法描述
    public static String description;
    public LogCustom logCustom = new LogCustom(this.getClass().getSuperclass());

    @BeforeTest
    @Parameters({"driverName", "nodeURL", "appName", "deviceId", "deviceName", "sdkVersion", "appMainPackage", "appActivity", "platformName"})
    public void setup(String driverName, String nodeURL, String appName, String deviceId, String deviceName, String sdkVersion, String appMainPackage, String appActivity, String platformName) throws MalformedURLException {
        logCustom.info("------------------开始执行测试---------------");
        //启动appium server
//		ElementAction action=new ElementAction();
//		logCustom.info("通过cmd命令启动appium server");
//		try {
//			String cmd="appium -a " +
//					nodeURL.split(":")[0]+
//					"  -p "+nodeURL.split(":")[1]+" -U "+deviceId;
//			System.out.println(cmd);
//			action.executeCmd(cmd);
//			action.sleep(10);
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
        if (nodeURL.equals("") || nodeURL.isEmpty()) {
            logCustom.error("appium url 没有设置");
        } else {
            logCustom.info("读取xml配置：Mobile Driver:" + driverName + "；Appium Server:" + "http://" + nodeURL + "/wd/hub" + "设备Id：" + deviceId + "设备名字：" + deviceName);
            try {
                //Process process=Runtime.getRuntime().exec("cmd.exe /c appium");
                //System.out.println(process.toString());
                //this.driver=setRemoteDriver(driverName, nodeURL, appName, deviceName, sdkVersion, appMainPackage, appActivity,platformName);
                driver = setRemoteDriver(driverName, nodeURL, appName, deviceId, deviceName, sdkVersion, appMainPackage, appActivity, platformName);
                logCustom.info(driver.getCapabilities().toString());
                System.out.println(driver.manage().logs().toString());
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
                logCustom.error("appium环境配置失败" + e.getMessage());
            }
        }

    }

    public void setisQuitDivices(Boolean b) {
        this.isQuitDivices = b;
    }

    @AfterTest
    public void tearDown() {
        try {
            if (isQuitDivices) {
                this.driver.quit();
            }
            ElementActionApp action = new ElementActionApp();
            logCustom.info("关闭appium server");
            action.executeCmd("taskkill /f/im cmd.exe");
            logCustom.info("-------------结束测试，并关闭退出driver及appium server-------------");
        } catch (Exception e) {
            logCustom.error(e.getMessage());
        }

    }

    /**
     * @param driverName     driverName
     * @param nodeURL        appium URL
     * @param appName        app文件名
     * @param deviceName     设备名字
     * @param sdkVersion     安卓版本
     * @param appMainPackage app主包名
     * @param appActivity    app主类名
     * @param platformName   设备平台    真机：Android AVD虚拟设备：Android Emulator
     * @return
     * @throws MalformedURLException
     */
    private AndroidDriver setRemoteDriver(String driverName, String nodeURL, String appName, String devieceId, String deviceName, String sdkVersion, String appMainPackage, String appActivity, String platformName) throws MalformedURLException {
        //获取app路径
        System.out.println(System.getProperty("user.dir"));
        File classRootPath = new File(System.getProperty("user.dir"));
        File appDir = new File(classRootPath, "apps");
        File app = new File(appDir, appName);
        switch (driverName) {
            case "AndroidDriver":
                //设置自动化相关参数
                DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
                desiredCapabilities.setCapability("platformName", platformName);
                desiredCapabilities.setCapability("uuid", devieceId);
                desiredCapabilities.setCapability("deviceName", deviceName);
                desiredCapabilities.setCapability("platformVersion", sdkVersion);
                //设置app路径
//				desiredCapabilities.setCapability("app", app.getAbsolutePath());
                //设置app主包名和主类名
//				desiredCapabilities.setCapability("appMainPackage", appMainPackage);
                desiredCapabilities.setCapability("appPackage", appMainPackage);
                desiredCapabilities.setCapability("appActivity", appActivity);
                //设置支持中文输入
                desiredCapabilities.setCapability("unicodeKeyboard", "True");
                desiredCapabilities.setCapability("resetKeyboard", "True");
                desiredCapabilities.setCapability("noSign", "True");
                //初始化driver
                driver = new AndroidDriver(new URL("http://" + nodeURL + "/wd/hub"), desiredCapabilities);
                break;
            default:
                //设置自动化相关参数
                DesiredCapabilities desiredCapabilities2 = new DesiredCapabilities();
                desiredCapabilities2.setCapability("platformName", "Android");
                desiredCapabilities2.setCapability("automationName", "Appium");
                desiredCapabilities2.setCapability("uuid", devieceId);
                desiredCapabilities2.setCapability("deviceName", deviceName);
                desiredCapabilities2.setCapability("platformVersion", sdkVersion);
                //设置app路径
//				desiredCapabilities2.setCapability("app", app2.getAbsolutePath());
                //设置app主包名和主类名
                desiredCapabilities2.setCapability("appMainPackage", "com.cheewo.lifefamily");
                desiredCapabilities2.setCapability("appActivity", ".MainActivity");
                //设置支持中文输入
                desiredCapabilities2.setCapability("unicodeKeyboard", "True");
                desiredCapabilities2.setCapability("resetKeyboard", "True");
                desiredCapabilities2.setCapability("noSign", "True");
                //初始化driver
                driver = new AndroidDriver(new URL("http://" + nodeURL + "/wd/hub"), desiredCapabilities2);
                break;
        }
        return driver;
    }

    public static void main(String args[]) {
        WebDriver driver2 = new FirefoxDriver();
        driver2.get("http://www.baidu.com");
    }


}
